<template>
  <div class="td-patient-delection-test-item-package">
    <div class="test-item-package-list">
      <div v-for="(item,index) in testItemPackageList" :key="index" @click="selectPackage(item)">
        <p>
          <span class="check">
            <i
              class="fa"
              :class="{'fa-circle':item.check,'fa-circle-thin':!item.check}"
              aria-hidden="true"
            ></i>
          </span>
          <span>{{item.label}}</span>
          <span>小计：{{item.total}}元</span>
        </p>
        <div v-for="(arg,j) in item.itemList" :key="j">
          <span>{{arg.code}}</span>
          <span>
            {{arg.label}}
            <label>({{arg.gander}})</label>
          </span>
          <span>{{arg.price}}元</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import testItemService from "../../../../_services/testItem.service";
import testItemPackageService from "../../../../_services/testItemPackage.service";
import testItemTypeService from "../../../../_services/testItemType.service";
import Enumerable from "linq";
import { mapActions } from "vuex";
export default {
  name: "TianDingPatientDelectionTestItemPackageComponent",
  data() {
    return {
      testItemList: [],
      testItemPackageList: [],
      selectTestItemPackageList: [],
      selectTestItemTypeList: [],
      selectTestItemList: [],
      total: 0
    };
  },
  mounted() {
    this.selectTestItemPackageList = this.$store.state.item.packageList;
    this.selectTestItemTypeList = this.$store.state.item.testItemTypeList;
    this.selectTestItemList = this.$store.state.item.testItemList;
    this.getTestPackageList();
  },
  methods: {
    getTestPackageList() {
      this.indicator$.open("正在加载...");

      this.testItemPackageList = [];

      let testItemType = testItemTypeService.getTestItemTypeList();
      let testItem = testItemService.getTestItemList();
      let testItemPackage = testItemPackageService.getTestPackageList();

      this.http$.all([testItemType, testItem, testItemPackage]).then(
        this.http$.spread(
          (testItemTypeResponse, testItemResponse, testItemPackageResponse) => {
            //对检测项目类型进行排序  根据排序值进行升序  再根据使用次数进行降序
            let testItemTypeList = Enumerable.from(testItemTypeResponse.Data)
              .orderBy(e => e.Sorting)
              .orderByDescending(e => e.UsedNum)
              .toArray();

            let testItemList = Enumerable.from(testItemResponse.Data).toArray();

            let packageList = Enumerable.from(
              testItemPackageResponse.Data
            ).toArray();

            if (testItemTypeList.length !== 0) {
              testItemTypeList.forEach(element => {
                element.label = element.TestItemTypeName;
                element.value = element.TypeID;
                element.gender = element.IsMan === 0 ? "女" : "男";
                element.colorText = "黄";

                switch (element.TypeColor) {
                  case 0:
                    element.colorText = "黄";
                    break;
                  case 1:
                    element.colorText = "紫";
                    break;
                  case 2:
                    element.colorText = "红";
                    break;
                  case 3:
                    element.colorText = "绿";
                    break;
                }
                element.check = false;

                let check = this.selectTestItemTypeList.findIndex(
                  e => e.value === element.value
                );
                element.check = check === -1 ? false : true;

                if (testItemList.length !== 0) {
                  let testItemArray = testItemList.filter(
                    (item, index, array) => {
                      item.label = item.ItemName;
                      item.value = item.TestItemID;
                      item.code = item.ItemCode;
                      item.price = this.utils$.subTransfer(item.ItemPrice);
                      item.check = false;
                      item.gender = element.gender;
                      let ch = this.selectTestItemList.findIndex(
                        e => e.value === item.value
                      );
                      item.check = ch === -1 ? false : true;

                      return item.TestItemTypeID === element.TypeID;
                    }
                  );

                  element.itemList = Enumerable.from(testItemArray)
                    .orderBy(e => e.Sorting)
                    .orderByDescending(e => e.UsedNum)
                    .toArray();
                }

                if (element.itemList.length !== 0) {
                  this.testItemList.push(element);
                }
              });
            }

            if (packageList.length !== 0) {
              packageList.forEach(element => {
                element.label = element.PackageName;
                element.value = element.PackageID;
                element.check = false;

                let check = this.selectTestItemPackageList.findIndex(
                  e => e.value === element.value
                );
                element.check = check === -1 ? false : true;
                let total = 0;
                let itemList = [];
                testItemList.forEach(ele => {
                  ele.label = ele.ItemName;
                  ele.value = ele.TestItemID;
                  ele.code = ele.ItemCode;
                  ele.price = this.utils$.subTransfer(ele.ItemPrice);

                  ele.gander =
                    testItemTypeList.find(e => e.TypeID === ele.TestItemTypeID)
                      .IsMan === 0
                      ? "女"
                      : "男";
                  ele.check = false;
                  if (element.ItemIDs.includes(ele.value)) {
                    total += parseFloat(ele.price);
                    itemList.push(ele);
                  }
                });

                element.total = parseFloat(total).toFixed(2);

                element.itemList = Enumerable.from(itemList)
                  .orderBy(e => e.Sorting)
                  .orderByDescending(e => e.UsedNum)
                  .toArray();

                this.testItemPackageList.push(element);
              });
            }

            this.indicator$.close();
          }
        )
      );
    },
    selectPackage(item) {
      item.check = !item.check;
      item.itemList.filter(e => (e.check = item.check));
      let index = this.selectTestItemPackageList.findIndex(
        e => e.value === item.value
      );
      if (item.check) {
        if (index === -1) {
          this.selectTestItemPackageList.push(item);
          item.itemList.forEach(element => {
            let i = this.selectTestItemList.findIndex(
              e => e.value === element.value
            );
            if (i === -1) {
              this.selectTestItemList.push(element);
            }
          });
        }
      } else {
        if (index !== -1) {
          this.selectTestItemPackageList.splice(index, 1);
          item.itemList.forEach(element => {
            let i = this.selectTestItemList.findIndex(
              e => e.value === element.value
            );
            if (i !== -1) {
              this.selectTestItemList.splice(i, 1);
            }
          });
        }
      }

      this.testItemList.forEach(ele => {
        ele.itemList.forEach(el => {
          let j = this.selectTestItemList.findIndex(e => e.value === el.value);
          el.check = j === -1 ? false : true;
        });

        let itemCheckList = ele.itemList.filter(e => e.check);
        ele.check = itemCheckList.length === ele.itemList.length;
        let n = this.selectTestItemTypeList.findIndex(
          e => e.value === ele.value
        );
        if (ele.check) {
          if (n === -1) {
            this.selectTestItemTypeList.push(ele);
          }
        } else {
          if (n !== -1) {
            this.selectTestItemTypeList.splice(n, 1);
          }
        }
      });

      let state = {
        selectTestItemTypeList: this.selectTestItemTypeList,
        selectTestItemList: this.selectTestItemList,
        selectTestItemPackageList: this.selectTestItemPackageList,
        total: this.total
      };

      this.calcTotal();
    },
    calcTotal() {
      this.total = 0;
      if (this.selectTestItemList.length !== 0) {
        this.selectTestItemList.forEach(e => {
          this.total += parseFloat(e.price);
        });
      }

      let state = {
        selectTestItemTypeList: this.selectTestItemTypeList,
        selectTestItemList: this.selectTestItemList,
        selectTestItemPackageList: this.selectTestItemPackageList,
        total: this.total
      };
      this.testItemAction(state);
    },
    ...mapActions(["testItemAction"])
  }
};
</script>
